package cn.com.tpqn;

import com.elite.groovy.kh.KHDBConfig;
import com.elite.groovy.kh.KHUtil;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.jdbc.core.JdbcTemplate;

//该测试类是用于提示客户如何生成秘钥，并调用接口的案例类，客户可在自己的类中使用该类内的代码内容
public class sessionDetail {
    private static final Log log = LogFactory.getLog(sessionDetail.class);
    private static JSONObject rec_Config = new JSONObject();

    public static String CCODURL = "";
    public static String Secret_Key = "";
    public static String Secret_Id = "";
    public static String EntID = "";
    public static String ProxyIP = "";
    public static String ProxyPort = "";

    public static String main(JdbcTemplate jt, String startTime, String endTime, String pageNum) throws Exception {

        // 初始化参数
        EntID = "1909090002";
        rec_Config = KHUtil.SFIsNull(rec_Config.toString()) ? JSONObject.fromObject(KHDBConfig.getParamConfig(jt, "REC_CFG-" + EntID)) : rec_Config;

        CCODURL = KHUtil.GetSFJV(rec_Config, "CCODURL");

        Secret_Key = KHUtil.GetSFJV(rec_Config, "SECRET_KEY");
        Secret_Id = KHUtil.GetSFJV(rec_Config, "SECRET_ID");
        ProxyIP = KHUtil.GetSFJV(rec_Config, "ProxyIP");
        ProxyPort = KHUtil.GetSFJV(rec_Config, "ProxyPort");

        String resultInfo = "";
        long currentTime = System.currentTimeMillis();
        String authorization = "/customWebservice/mvc/sessionDetail/query.do?Nonce=" + currentTime + "&Timestamp=" + currentTime / 1000 + "&SecretId=" + Secret_Id;

        // 调用接口生成秘钥
        @SuppressWarnings("static-access") String Signature = new QnSignUitl().sign(authorization, Secret_Key);

        // URI是要调用的接口地址-通过SessionID：475d425b2d00065b，5142338086570564491
        String URI = CCODURL + "/customWebservice/mvc/sessionDetail/query.do?entId=" + EntID + "&startTime=" + startTime + "&endTime=" + endTime + "&pageNum=" + pageNum + "&Signature=" + Signature;

        CloseableHttpClient httpclient = null;

        if (ProxyIP == null || ProxyIP.length() == 0) {
            httpclient = HttpClients.createDefault();

        } else {

            // 设置代理IP、端口、协议（请分别替换）
            HttpHost proxy = new HttpHost(ProxyIP, Integer.parseInt(ProxyPort), "http");

            // 把代理设置到请求配置
            RequestConfig defaultRequestConfig = RequestConfig.custom().setProxy(proxy).build();

            // 实例化CloseableHttpClient对象
            httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
        }

        try {

            HttpPost httpPost = new HttpPost(URI);
            httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
            httpPost.setHeader("Accept", "*/*");
            httpPost.setHeader("Authorization", authorization);
            CloseableHttpResponse response2 = httpclient.execute(httpPost);

            try {
                log.debug(response2.getStatusLine());
                log.debug(response2.getEntity().getContentType());
                HttpEntity entity2 = response2.getEntity();
                resultInfo = EntityUtils.toString(entity2, "utf-8");
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
        return resultInfo;
    }
}